
const router = require('koa-router')()
const data = require('./../config/datapull')
// const { query } = require('./../config/dbPool');
// const { QUERY_SQL, INSERT_SQL, UPDATE_SQL, DELETE_SQL } = require('./../config/dbSQL');
const axios = require('axios');


async function searchNearbyMarkets(latitude, longitude) {
	try {
	  const apiKey = 'c9831a6e3f5ea2b3f612fdfe989cee03'; // 替换为你的高德地图 API 密钥
  
	  const response = await axios.get(
		`https://restapi.amap.com/v3/place/around?key=${apiKey}&location=${longitude},${latitude}&keywords=菜市场&types=&radius=2000&offset=10&page=1&extensions=all`
	  );
  
	  const nearbyMarkets = response.data.pois;
	  return nearbyMarkets
	 
	} catch (error) {
	  console.error('获取附近菜市场失败：', error.message);
	}
  }




router.get('/', async (ctx, next) => {
	// await ctx.render('index', {
	//   title: 'Hello Koa 2!'
	// })
	// const data = await test;
	// ctx.body = {
	//     data,
	// };
	var arr = []

})
async function getMarketPhotos(markets) {
	try {
	  const apiUrl = 'https://restapi.amap.com/v3/place/detail';
	  const photos = [];
  
	  for (const market of markets) {
		const params = {
		  key: apiKey,
		  id: market.id
		};
  
		const response = await axios.get(apiUrl, {
		  params: params
		});
  
		const marketDetail = response.data;
		const photo = marketDetail.photo;
		photos.push(photo);
	  }
  
	  console.log('菜市场图片：', photos);
	} catch (error) {
	  console.error('获取菜市场图片失败：', error.message);
	}
  }
  
//搜索附近菜市场
router.post('/location', async (ctx, next) => {

	const lon=ctx.request.body.longitude
	const lat=ctx.request.body.latitude
	const apiKey = 'c9831a6e3f5ea2b3f612fdfe989cee03'; // 替换为你的高德地图 API 密钥

    const response = await axios.get(
      `https://restapi.amap.com/v3/place/around?key=${apiKey}&location=${lon},${lat}&keywords=菜市场&types=&radius=2000&offset=10&page=1&extensions=all`
    );
    const nearbyMarkets = response.data.pois;


	const photos = [];
	nearbyMarkets.forEach(element => {
		const photo = element.photos[0].url;
		photos.push(photo);
	});
	  

	
	// const nearbyMarkets=searchNearbyMarkets(lon,lat)
	ctx.body={
		nearbyMarkets,
		photos
	}
	
})

router.get('/json', async (ctx, next) => {
	ctx.body = {
		title: 'koa2 json'
	}
})

module.exports = router
